package com.gpj.sqlbuilder.utils;

import com.gpj.sqlbuilder.store.common.MarkIndex;
import org.apache.commons.lang3.StringUtils;

/**
 * @className TextUtil
 * @description
 *   TODO
 * @author GPJ
 * @date 2021/3/30 14:31
 * @version 1.0
 **/
public class TextUtils {
	public static String substr(int start, int end, String datas) {
	    if(end < 0){
	    	end = datas.length() + end;
		}
	    return datas.substring(start, end);
	}

	public static StringBuffer substr(int start, int end, StringBuffer datas) {
		if(end < 0){
			end = datas.length() + end;
		}
		return new StringBuffer(datas.substring(start, end));
	}
	
	public static boolean isRound(String mark, String datas){
		String[] marks = mark.split("\\*");
		return datas.startsWith(marks[0]) && mark.endsWith(marks[1]);
	}
	
	public static String roundValue(String mark, String datas){
		if(datas.length() <= mark.length() - 1) return "";
		String[] marks = mark.split("\\*");
		return substr(marks[0].length(), -1 * marks[1].length(), datas);
	}

	/**
	 * 查找成对的标记符的边界,例如<for>与</for>
	 * @param datas 要查找的字符串
	 * @param startMark 开始标记符
	 * @param endMark 结束标记符
	 * @param offset 起始查找位置
	 */
	public static MarkIndex findMarkIndex(
			StringBuffer datas, String startMark, String endMark, int offset){
		return findMarkIndex(datas.toString(), startMark, endMark, offset);
	}

	/**
	 * 查找成对的标记符的边界,例如<for>与</for>
	 * @param datas 要查找的字符串
	 * @param startMark 开始标记符
	 * @param endMark 结束标记符
	 * @param offset 起始查找位置
	 */
	public static MarkIndex findMarkIndex(
			String datas, String startMark, String endMark, int offset){
		int start;
		if((start = datas.indexOf(startMark, offset)) == -1) return null;
		int stack = 0;

		do{
			int startMarkIndex = datas.indexOf(startMark, offset);
			int endMarkIndex = datas.indexOf(endMark, offset);

			if(startMarkIndex == -1 || startMarkIndex >= endMarkIndex){
				stack--;
				offset = endMarkIndex + endMark.length();
			}else if(startMarkIndex < endMarkIndex){
				offset = startMarkIndex + startMark.length();
				stack++;
			}
		}while (stack > 0);
		return new MarkIndex(start, offset + endMark.length() - 1 - startMark.length());
	}

	/**
	 * 查找成对的标记符的末尾位置,例如<for>与</for>中</for>标记的位置
	 * @param datas 要查找的字符串
	 * @param startMark 开始标记符
	 * @param endMark 结束标记符
	 * @param offset 起始查找位置
	 */
	public static int findEndMarkIndex(
			StringBuffer datas, String startMark, String endMark, int offset){
		return findEndMarkIndex(datas.toString(), startMark, endMark, offset);
	}

	/**
	 * 查找成对的标记符的末尾位置,例如<for>与</for>中</for>标记的位置
	 * @param datas 要查找的字符串
	 * @param startMark 开始标记符
	 * @param endMark 结束标记符
	 * @param offset 起始查找位置
	 */
	public static int findEndMarkIndex(
			String datas, String startMark, String endMark, int offset){
		MarkIndex index = findMarkIndex(datas, startMark, endMark, offset);
		return index != null?index.getEnd():-1;
	}

	/**
	 * 查找标记符号所包裹的字符串,并将其替换为解析函数产生的值,同时去掉标记符号（如:${},#[]）
	 * @param mark 标记符号(以*分隔)
	 * @param template 需要查找替换的模板
	 * @param handler 字符串解析函数
	 */
	public static StringBuffer findAndReplace(String mark, StringBuffer template, MarkHandle handler) {
		String[] marks = mark.split("\\*");
		int index = 0;
		while (index < template.length()){
			int start = template.indexOf(marks[0], index);
			if(start == -1) break;
			int end = template.indexOf(marks[1], start);
			String value = template.substring(start + 2, end);
			index = end;

			String replaceVaule = handler.handle(start, end, value);

			if(replaceVaule == null) continue;

			if(StringUtils.isBlank(replaceVaule)
					&& StringUtils.equals(" ", template.substring(end + 1, end + 2))){
				template = template.replace(start, end + 2, replaceVaule);
			}else{
				template = template.replace(start, end + 1, replaceVaule);
			}
			index = 0;
		}
		return template;
	}
}
